package com.example.kotlin.utils;

import android.content.Context;
import android.graphics.Typeface;
import android.text.Spannable;
import android.text.SpannableString;

import com.example.kotlin.MyApplication;

/**
 * 描述：获取自定义字体typefacespan的单例 <br/>
 */
public class FontsUtil {

    public static FontsUtil fontsUtil;

    public Typeface numTypefaceBold;
    public Typeface numTypefaceNormal;

    public FontsUtil() {
        // 字体资源放在内存中，避免反复读取浪费资源
        numTypefaceBold = Typeface.createFromAsset(MyApplication.getContext().getAssets(),
                "fonts/DIN Alternate Bold.ttf");
        numTypefaceNormal = Typeface.createFromAsset(MyApplication.getContext().getAssets(),
                "fonts/DIN Regular.otf");

    }

    /**
     * <br/>
     * 概述：字体单例，避免反复读取 <br/>
     *
     * @param context <br/>
     * @return
     */
    public static FontsUtil getInstance(Context context) {
        if (fontsUtil == null) {
            fontsUtil = new FontsUtil();
        }
        return fontsUtil;
    }

    public static FontsUtil getInstance() {
        if (fontsUtil == null) {
            fontsUtil = new FontsUtil();
        }
        return fontsUtil;
    }

    /**
     * <br/>
     * 概述：获取数字的字体typefacespan <br/>
     *
     * @param
     * @return
     */
    public MyTypefaceSpan getMyNumTypefaceSpan() {
        return new MyTypefaceSpan(numTypefaceBold);
    }

    /**
     * 转换成数字字体
     *
     * @param string
     * @return
     */
    public SpannableString formatNumTypeFace(String string) {
        SpannableString spannable = new SpannableString(string);
        spannable.setSpan(getMyNumTypefaceSpan(), 0, string.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        return spannable;
    }

}